package test.test.one;

import test.test.one.objects.ob_exit;
import test.test.one.objects.ob_pickable;
import test.test.one.objects.ob_player;
import test.test.one.objects.tools.Speed;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * @deprecated used when the game had a single level
 * 
 * for reference only
 */

//TODO delete or finish documentation
public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback {

	/*
	 * first is ALWAYS TAG
	 */
	
	private static final String TAG = MainGamePanel.class.getSimpleName();
	
	/*
	 * variable declaration
	 */
	
	private MainThread thread;
	private ob_exit bt_exit;
	private ob_player player;
	//items
	private ob_pickable item01;
	private ob_pickable item02;
	
	//temp
	private boolean i1,i2=false;
	
	//TODO private ob_pickable items[] = {item01,item02};
	//new player position
	private float new_pos;
	//coordinate
	private int coor=100;
	

	/*
	 * methods
	 */
	
	public MainGamePanel(Context context) {
		super(context);
		// adding the callback (this) to the surface holder to intercept events
		getHolder().addCallback(this);
		
		
		//create objects and load bitmap
		bt_exit= new ob_exit(BitmapFactory.decodeResource(getResources(), R.drawable.exit), 20, 20);
		player = new ob_player(BitmapFactory.decodeResource(getResources(), R.drawable.yao), 
				TestActivity.GAMEWIDTH/2, TestActivity.GAMEHEIGHT-50);
				//100, 100);
		item01= new ob_pickable(BitmapFactory.decodeResource(getResources(), R.drawable.sandwich), 100, 200);
		item02= new ob_pickable(BitmapFactory.decodeResource(getResources(), R.drawable.sandwich), 250, 300);
		
		// create the game loop thread
		//thread = new MainThread(getHolder(), this);
				
		// make the GamePanel focusable so it can handle events
		setFocusable(true);
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// at this point the surface is created and
		// we can safely start the game loop
		thread.setRunning(true);
		thread.start();
	}
	
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.d(TAG, "Surface is being destroyed");
		// tell the thread to shut down and wait for it to finish
		// this is a clean shutdown
		boolean retry = true;
		while (retry) {
			try {
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
				// try again shutting down the thread
			}
		}
		Log.d(TAG, "Thread was shut down cleanly");
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			
			//handle all objects
			player.handleActionDown((int)event.getX(), (int)event.getY());
			bt_exit.handleActionDown((int)event.getX(), (int)event.getY());
			item01.handleActionDown((int)event.getX(), (int)event.getY());
			item02.handleActionDown((int)event.getX(), (int)event.getY());
						
			//end game
			if (bt_exit.isTouched()){
				
				
				thread.setRunning(false);
				//((Activity)getContext()).finish();
				
				//MainGamePanel mgp2 = mgp2=new MainGamePanel(this);
				//((Activity) getContext()).setContentView(TestActivity.mgp2);
				
				
				
			}
			else{
				//TODO fix blocks
					
					if(item01.isTouched()){
						item01.setX(coor+=50);
						item01.setY(50);
						i1=true;
						return true;
						
					}
					if(item02.isTouched()){
						item02.setX(coor+=50);
						item02.setY(50);
						i2=true;
						return true;
					}
				
				
							
				Log.d(TAG, "x=" + event.getX() + ",y=" + event.getY());
				new_pos=event.getX();
				player.setSpeed(new Speed(3,0));
				
				if(new_pos<player.getX()){
					player.getSpeed().toggleXDirection();
				}
			}
			}
			
				
		return true;
		}
	

	public void render(Canvas canvas) {
		canvas.drawColor(Color.WHITE);
		player.draw(canvas);
		bt_exit.draw(canvas);
		item01.draw(canvas);
		item02.draw(canvas);
		
	}
	public void update() {
		
		// TODO looping 
		if(i1&&i2){
			
			//Intent i=new Intent(MainGamePanel.this, TestActivity.class);
			//startActivity(i);
			
			//thread.setRunning(false);
			//thread.finish();
		}
		
		
		if(player.getX()<new_pos+3 && player.getX()>new_pos-3){
			player.setSpeed(new Speed());
		}
		player.update();
	
	}
}
